<?php
class CluesAction extends CommonAction {

    public function __construct(){
        parent::__CONSTRUCT();
    }

    public function add_clues(){
        $mobile = $this->_post("mobile");
        if( empty($mobile) ){
            $this->ajaxError('手机号码不能为空！');
        }
        if(!preg_match('/^1[3-9]\d{9}$/', $mobile)){
            $this->ajaxError('手机号码格式不正确！');
        }
        $sourceArr = C('CLUES_SOURCE');
        $source = intval($this->_post("source"));
        if( empty($source) || !isset($sourceArr[$source]) ){
            $this->ajaxError("非法请求！");
        }
        $trademarkSn = $this->_post("trademark_sn");
        $trademarkName = $this->_post("trademark_name");
        $trademarkPrice = $this->_post("trademark_price");
        //查询手机号今天是否提交过，同个手机号,同1天，同1个渠道，最多提交2次！
        $count = M("user_clues")
            ->where(array('mobile'=>$mobile, 'source'=>$source, 'created_at'=>array('between',array(date("Y-m-d 00:00:00"),date("Y-m-d 23:59:59")))))
            ->count();
        if( $count > 2 ){
            $this->ajaxError('同一个手机号每天最多提交2次！请耐心等待工作人员进行回访！');
        }
        //获取IP地址所在地
        import('@.ORG.Juhe.Juhe');
        $ip_address = Juhe::getIpAddress(get_client_ip());
        //添加线索入库
        $userClues = array();
        $userClues['trademark_sn'] = $trademarkSn;
        $userClues['trademark_name'] = $trademarkName;
        $userClues['trademark_price'] = $trademarkPrice;
        $userClues['mobile'] = $mobile;
        $userClues['ip'] = get_client_ip();
        $userClues['ip_address'] = $ip_address;
        $userClues['message_status'] = 1;
        $userClues['status'] = 1;
        $userClues['source'] = $source;
        $userClues['created_at'] = date("Y-m-d H:i:s");
        $F = M("user_clues")->add($userClues);
        if( !$F ){
            $this->ajaxError('提交失败，请联系拨打网站官方客服电话进行咨询！');
        }
        $clueid = M("user_clues")->getLastInsID();
        //发送消息提醒
        import('@.ORG.WorkWechat.WorkWechat');
        $markDown = "`新线索`\n";
        $markDown .= ">平台名称：<font color=\"info\">闹闹大师</font>\n";
		$markDown .= ">反馈编号：<font color=\"info\">{$clueid}</font>\n";
        $markDown .= ">线索来源：<font color=\"info\">{$sourceArr[$source]}</font>\n";
		$markDown .= ">商标编号：{$userClues['trademark_sn']}\n";
		$markDown .= ">商标名称：{$userClues['trademark_name']}\n";
		$markDown .= ">商标价格：{$userClues['trademark_price']}\n";
		$markDown .= ">IP 地址：{$userClues['ip']}\n";
		$markDown .= ">手机号码：<font color=\"info\">{$userClues['mobile']}</font>\n";
		$markDown .= ">反馈时间：<font color=\"comment\">{$userClues['created_at']}</font>\n";
		$markDown .= ">请及时进行回访。\n";
        $accessToken = WorkWechat::appChatSendMessage("operation1","markdown",$markDown,false);
        //添加成功，发送企业微信通知
        $this->ajaxSuccess('提交成功，商标顾问会尽快与您联系，请保持手机畅通！');
    }

    /**
     * 查询商标分类下的群组列表
     * @param $cateId
     * @return false|mixed|string|null
     */
    private function getGroup($cateId){
        return M("trademarks_group")->where(array('cate_id'=>$cateId))->select();
    }
}
?>